<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
             label-width="68px">
      <el-form-item label="库存产品" prop="goodsId">

        <el-select v-model="queryParams.goodsId" placeholder="请选择产品"
                   remote reserve-keyword :remote-method="searchGoods" @change="selectGoods" filterable clearable>
          <el-option v-for="item in goodsList"
                     :key="item.id" :label="item.label" :value="item.id"/>
        </el-select>
      </el-form-item>
      <el-form-item label="内部名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入名称"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="内部编号" prop="code">
        <el-input v-model="queryParams.code" placeholder="请输入编号"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="内部型号" prop="model">
        <el-input v-model="queryParams.model" placeholder="请输入型号"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                   v-hasPermi="['develop:GoodsOrderInner:add']">新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single"
                   @click="handleUpdate" v-hasPermi="['develop:GoodsOrderInner:edit']">修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-popconfirm title="确认删除选择的数据项？" @confirm="handleDelete({})">
          <el-button slot="reference" type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
                     v-hasPermi="['develop:GoodsOrderInner:remove']">删除
          </el-button>
        </el-popconfirm>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini"
                   @click="handleExport" v-hasPermi="['develop:GoodsOrderInner:export']">导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange" ref="table"
              :max-height="tableMaxHeight">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="序号" type="index" width="50" align="center" />
      <el-table-column label="库存名称" align="center" prop="params.name"/>
      <el-table-column label="库存编号" align="center" prop="params.code"/>
      <el-table-column label="库存型号" align="center" prop="params.model"/>
      <el-table-column label="内部名称" align="center" prop="name"/>
      <el-table-column label="内部编号" align="center" prop="code"/>
      <el-table-column label="内部型号" align="center" prop="model"/>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit"
                     @click="handleUpdate(scope.row)" v-hasPermi="['develop:GoodsOrderInner:edit']">修改
          </el-button>
          <el-popconfirm :title="`确认删除当前数据项？`" @confirm="handleDelete(scope.row)"
                         style="margin-left: 10px;">
            <el-button slot="reference" size="mini" type="text" icon="el-icon-delete"
                       v-hasPermi="['develop:GoodsOrderInner:remove']">删除
            </el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize" @pagination="getList"/>

    <goodsOrderInner-form ref="form" @ok="getList"></goodsOrderInner-form>
  </div>
</template>

<script>
import {deleteAction, getAction} from '@/api/manage'
import GoodsOrderInnerForm from '@/views/develop/goodsOrderInner/module/GoodsOrderInnerForm'

export default {
  name: "GoodsOrderInnerList",
  components: {
    GoodsOrderInnerForm,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 厂内编码表格数据
      dataList: [],
      goodsList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        goodsId: null,
        name: null,
        code: null,
        model: null,
      },
      url: {
        list: '/goodsOrderInner/goodsOrderInner/list',
        remove: '/goodsOrderInner/goodsOrderInner/',
        export: '/goodsOrderInner/goodsOrderInner/export',
      },
      tableMaxHeight: window.innerHeight - 320,
    };
  },
  created() {
    this.getList();
    window.addEventListener('resize', this.getTableHeight);
  },
  destroyed() {
    window.removeEventListener('resize', this.getTableHeight)
  },
  methods: {
    /** 查询厂内编码列表 */
    getList() {
      this.loading = true;
      getAction(this.url.list, this.queryParams).then(response => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getTableHeight() {
      this.tableHeight = window.innerHeight - 320
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$refs.form.handleAdd();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$refs.form.handleUpdate(row);
    },

    searchGoods(query) {
      if (query !== '') {
        this.loading = true; // 开始加载数据
        let params = {"queryLabel": query, "pageNum": 1, "pageSize": 50,};
        getAction('/goodsOrderIn/goodsOrderIn/list', params).then(response => {
          this.goodsList = response.rows;
          for (let i = 0; i < this.goodsList.length; i++) {
            this.goodsList[i].label = this.goodsList[i].name + '-' + this.goodsList[i].model + '-' + this.goodsList[i].code;
          }
        })
      } else {
        this.goodsList = [];
      }
    },

    selectGoods(value) {
      for (let i = 0; i < this.goodsList.length; i++) {
        if (this.goodsList[i].id == value) {
          this.queryParams.goodsId = this.goodsList[i].id;
          this.$forceUpdate();
        }
      }
    },


    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      deleteAction(this.url.remove, ids).then(response => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(this.url.export, {
        ...this.queryParams
      }, `goodsOrderInner_${new Date().getTime()}.xlsx`)
    },
  }
};
</script>
